const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

// 检查是否有出生信息
const checkBirthInfo = () => {
  const birthInfo = wx.getStorageSync('birthInfo');
  return birthInfo && birthInfo.name && birthInfo.birthDate && birthInfo.birthTime && birthInfo.gender;
};

// 跳转到出生信息页面
const navigateToBirthInfo = (targetPage) => {
  wx.navigateTo({
    url: '/pages/birth-info/birth-info',
    success: () => {
      // 将目标页面路径保存到本地存储
      wx.setStorageSync('targetPage', targetPage);
    }
  });
};

// 获取出生信息，如果不存在则返回默认值
const getBirthInfo = () => {
  try {
    const birthInfo = wx.getStorageSync('birthInfo') || {};
    console.log('从本地存储获取的出生信息:', birthInfo); // 添加日志
    
    return {
      name: birthInfo.name || '未设置',
      birthDate: birthInfo.birthDate || '未设置',
      birthTime: birthInfo.birthTime || '未设置',
      gender: birthInfo.gender || '男'
    };
  } catch (e) {
    console.error('获取出生信息失败:', e); // 添加错误日志
    return {
      name: '未设置',
      birthDate: '未设置',
      birthTime: '未设置',
      gender: '男'
    };
  }
};

// 使用更简单的导出方式
module.exports = {
  formatTime,
  formatNumber,
  checkBirthInfo,
  navigateToBirthInfo,
  getBirthInfo
}
